package weaver.jobs.autolink.duss.utils;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.Map;
import java.util.Set;

/**
 * @author dusongsong
 * @date 2023年07月13日 14:13
 */
public class AutolinkUtil extends BaseBean {
    /**
     * @Description: 创建流程
     * @Param: [userid, curdate, requestMainMap]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/30
     */
    public String createGzrbWf(String userid, String curdate, Map<String, String> requestMainMap) {
        String workflowId = getWorkflowId("-60");
        String workflowName = "工作日报";
        String isNextFlow = "0";
        String requestid = "";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + curdate;
            WorkflowCreateHandler handler = new WorkflowCreateHandler();
            requestid = handler.WorkflowCreateByRequestMap(userid, workflowId, requestName, isNextFlow, requestMainMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestid;
    }

    /**
     * @Description: 创建流程
     * @Param: [userid, curdate, requestMainMap]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2022/8/30
     */
    public String createZzspdWf(String userid, String curdate, Map<String, String> requestMainMap) {
        String workflowId = getWorkflowId("-96");
        String workflowName = "转正审批单";
        String isNextFlow = "1";
        String requestid = "";
        try {
            ResourceComInfo rci = new ResourceComInfo();
            String requestName = workflowName + "-" + rci.getLastname(userid) + "-" + curdate;
            WorkflowCreateHandler handler = new WorkflowCreateHandler();
            requestid = handler.WorkflowCreateByRequestMap(userid, workflowId, requestName, isNextFlow, requestMainMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return requestid;
    }

    /**
     * 获取生效版本的workflowID
     *
     * @param formid
     * @return
     */
    public String getWorkflowId(String formid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select ifnull(activeVersionID,id) from workflow_base where formid=? group by ifnull(activeVersionID,id)", formid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 判断是否有请假流程
     *
     * @param resourceid
     * @param currentDate
     * @return
     */
    public boolean hasQjlc(String resourceid, String currentDate) {
        RecordSet rs = new RecordSet();
        boolean flag = true;
        rs.executeQuery("select requestid from formtable_main_37 where (ksrq<='" + currentDate + "' and '" + currentDate + "'<=jsrq) and sqr=?", resourceid);
        rs.next();
        String requestid = Util.null2String(rs.getString("requestid"));
        if (!"".equals(requestid) && isGdByqjlc(requestid)) {
            rs.executeQuery("select duration from kq_flow_split_leave where fromdate=? and todate=? and requestid=?", currentDate, currentDate,requestid);
            rs.next();
            double duration = Util.getDoubleValue(rs.getString(1), -1);
            if (duration >= 8) {
                flag = false;
            }
        }
        return flag;
    }


    /**
     * 判断是否有加班流程
     *
     * @param resourceid
     * @param currentDate
     * @return
     */
    public boolean hasJblc(String resourceid, String currentDate) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select mainid from formtable_main_36_dt1 where ksrq='" + currentDate + "' and sjjbr=?", resourceid);
        rs.next();
        String mainid = Util.null2String(rs.getString("mainid"));
        rs.executeQuery("select requestid from formtable_main_36 where id=?", mainid);
        rs.next();
        String requestid = Util.null2String(rs.getString(1));
        return isGdByqjlc(requestid);
    }

    /**
     * 判断流程是否归档
     *
     * @param requestid
     * @return
     */
    public boolean isGdByqjlc(String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(requestid) from workflow_requestbase where requestid=? and currentnodetype=3", requestid);
        rs.next();
        return Util.getIntValue(rs.getString(1)) > 0;
    }

    /**
     * 判断人员是否有打卡记录
     *
     * @param requestid
     * @return
     */
    public boolean isScheduleSign(String requestid, String currendate) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select count(id) from HrmScheduleSign where userId=? and signDate=?", requestid, currendate);
        rs.next();
        return Util.getIntValue(rs.getString(1)) > 0;
    }

    /**
     * 更新明细表单信息
     */
    public boolean updateDetailData(String tablename, Map<String, Object> map) {
        String insertsql = "insert into " + tablename + "";
        String keyString = "";
        String valueString = "";
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            if (map.get(key) == null || "".equals(map.get(key).toString())) {
                continue;
            }
            keyString += key + ",";
            valueString += "'" + map.get(key).toString() + "',";
        }
        if (keyString.endsWith(",")) {
            keyString = keyString.substring(0, keyString.length() - 1);
        }
        if (valueString.endsWith(",")) {
            valueString = valueString.substring(0, valueString.length() - 1);
        }
        insertsql += "(" + keyString + ") values(" + valueString + ")";
        writeLog(">>>>>updateDetailData>>>>insertsql=" + insertsql);
        RecordSet rs = new RecordSet();
        return rs.executeUpdate(insertsql);
    }

    /**
     * 获取明细主键ID
     */
    public String getIdByRequestId(String tableName, String requestid) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + tableName + " where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 判断是否是工作日
     */
    public boolean isWorkDay(String currentDate) {
        boolean flag = true;
        RecordSet rs = new RecordSet();
        int week = DateUtil.getWeek(currentDate);
        rs.executeQuery("select count(id) from kq_HolidaySet where holidayDate=?", currentDate);
        rs.next();
        int count = Util.getIntValue(rs.getString(1));
        if (count > 0 || week > 5) {
            flag = false;
        }
        return flag;
    }

}
